#include "board_base.h"

#include "bsp_uart.h"
// #include "SEGGER_RTT.h"
// #include "ITM.h"
#include "tim.h"
#include "gpio.h"
#include "adc.h"
#include "task_main.h"
#include "stimer.h"
#include "log.h"
#include "bsp_uart.h"
// #include "w25qxx.h"
/*-----------------------------------------------------------------------
|                                define                                 |
-----------------------------------------------------------------------*/
#define BOARDTAG "Board"
#define Board_Firmware_ProjectName "STM32F030CCT6_SmartLock_Project"
#define Board_Firmware_Version STR(1.0)
#define Board_Firmware_Auther "liuzhihua"
#define Board_System_Frequnce SystemCoreClock
const char LINESTR[] = "--------------------------------------------------------------------------------\n\r";
#define STR(str) #str

/*-----------------------------------------------------------------------
|                               Function                                |
-----------------------------------------------------------------------*/

uint16_t ADC1Buf[ADC1_BUF_SIZE];

/**
 * @brief 记录归零，开始测量时间
 * @param timecnt 用来记录时间的变量
 * @retval uint32_t
 */
void bsp_MesureTimeStart(uint32_t *timecnt)
{
	*timecnt = HAL_GetTick();
}

/**
 * @brief 测量时间
 * @param old_counter 用来记录时间的变量
 * @retval uint32_t 上次使用到这次使用的时间(ms)
 */
uint32_t bsp_MesureTime(uint32_t *old_counter)
{
	uint32_t counter, couter_current;
	couter_current = HAL_GetTick();
	if (couter_current > *old_counter) // 没有溢出
		counter = couter_current - *old_counter;
	else // 发生过溢出
		counter = couter_current + 0XFFFFFFFF - *old_counter;
	*old_counter = couter_current;
	return (counter);
}

/**
 * @brief 测量时间us
 * @param old_counter 用来记录时间的变量
 * @retval uint16_t 上次使用到这次使用的时间(us)
 */
uint16_t bsp_MesureTimeUS(uint16_t *old_counter)
{
	uint16_t counter, couter_current;
	couter_current = __HAL_TIM_GET_COUNTER(&htim7);
	if (couter_current > *old_counter) // 没有溢出
		counter = couter_current - *old_counter;
	else // 发生过溢出
		counter = couter_current + 0XFFFF - *old_counter;
	*old_counter = couter_current;
	return (counter);
}

void bsp_delay_us(uint16_t us)
{
	uint32_t compare = __HAL_TIM_GET_COUNTER(&htim7) + us;
	if (compare > 0xFFFF)
	{
		while (__HAL_TIM_GET_COUNTER(&htim7) < 0xFFFF)
			;
		while (__HAL_TIM_GET_COUNTER(&htim7) < (compare - 0xFFFF))
			;
	}
	else
	{
		while (__HAL_TIM_GET_COUNTER(&htim7) < us)
			;
	}
}

#define buzzer_write(x) Pinx_Write(Buzzer, x);
#define buzzer_read() Pinx_ORead(Buzzer)

uint16_t TaskBuzzerID = 0;
void TaskBuzzer(const void *arg)
{
	LEDx_Write(LED2, 0);
	// buzzer_write(0);
}
void Beep(uint32_t time)
{
	stimer_task_stop(TaskBuzzerID);
	stimer_task_set_interval(TaskBuzzerID, time);
	stimer_task_start(TaskBuzzerID, 1, (void *)"TaskBuzzer");
	// buzzer_write(1);
	LEDx_Write(LED2, 1);
}
void board_info_output(void)
{
	LOGS("board base Start\n\r");
	LOGS(LINESTR);
	LOG_INFO(BOARDTAG, "ProjectName :  %s\n\r", Board_Firmware_ProjectName);
	LOG_INFO(BOARDTAG, "Version     :  %s\n\r", Board_Firmware_Version);
	LOG_INFO(BOARDTAG, "Auther      :  %s\n\r", Board_Firmware_Auther);
	LOG_INFO(BOARDTAG, "SystemClock :  %d\n\r", Board_System_Frequnce);
	// 	LOG_INFO(BOARDTAG, "Extern Flash: W25Q%d[ID:%d|FlashSize:%dKB]\n",w25qxx.BlockCount/2, w25qxx.ID, w25qxx.BlockCount * (w25qxx.BlockSize/1024));
	LOGS(LINESTR);
}

/**
 * @brief 板载资源初始化
 */
void board_base_init(void)
{
	UART_BSP_Init();
	BSP_VBAT_ADC_ENABLE();
	HAL_ADCEx_Calibration_Start(&hadc);
	// HAL_ADC_Start(&hadc);
	HAL_ADC_Start_DMA(&hadc, (uint32_t *)ADC1Buf, ADC1_BUF_SIZE);
	//	HAL_TIM_Base_Start_IT(&htim17);
	HAL_TIM_Base_Start(&htim7); // 计时和延时使用
	HAL_TIM_Base_Start(&htim1);
	LEDx_Write(LED1, 0);
	LEDx_Write(LED2, 0);
	LEDx_Write(LED3, 0);
}

/**
 * @brief  Period elapsed callback in non-blocking mode
 * @param  htim TIM handle
 * @retval None
 */
// void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
//{
////	if (htim == &htim17)
////	{
////		stimer_tick_increase();
////	}
////	if (htim == &htim6)
////	{
////		LLCC68Callback1ms();
////	}
//}

/**
 * @brief  EXTI line detection callbacks.
 * @param  GPIO_Pin: Specifies the pins connected EXTI line
 * @retval None
 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	// UNUSED(GPIO_Pin);

	//	if (GPIO_Pin == CMP_OVP_LV_Pin || GPIO_Pin == CMP_OVP_HV_Pin)
	//	{
	//		DCDC_MOS_DISABLE();
	//	}
}
